package com.ship.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.contract.bean.ConAllLabel;
import com.ship.contract.mapper.ConAllLabelMapper;
import com.ship.contract.service.ConAllLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ship.common.core.exception.BusException;

import java.util.List;

/**
 * @author tangshixuan
 * @date 2023-9-1
 */
@Service
public class ConAllLabelServiceImpl extends ServiceImpl<ConAllLabelMapper,ConAllLabel> implements ConAllLabelService {
    @Autowired
    private ConAllLabelMapper conAllLabelMapper;

    @Override
    public List<ConAllLabel> listByConId(Long id) {
        QueryWrapper<ConAllLabel> queryWrapper = new QueryWrapper<ConAllLabel>();
        queryWrapper.eq("con_id",id);
        queryWrapper.and(qw -> qw.isNull("label_id"));
        queryWrapper.and(qw -> qw.eq("user_id", SecurityUtils.getStringUserId()));
        return super.list(queryWrapper);
    }

    @Override
    public void updateByLabelId(Long id, String labelValue) {
        conAllLabelMapper.updateByLabelId(id,labelValue);
    }

    @Override
    public void saveContractLabel(ConAllLabel conAllLabel) {
        String labelValue = conAllLabel.getLabelValue();
        QueryWrapper<ConAllLabel> queryWrapper = new QueryWrapper<ConAllLabel>();
        queryWrapper.eq("con_id",conAllLabel.getConId());
        queryWrapper.eq("label_value",labelValue);
//        ConAllLabel exist = super.getOne(queryWrapper,false);
//        if(exist != null){
//            throw new BusException("该标签已经存在!", BaseResultCode.GENERAL_ERROR);
//        }
        super.save(conAllLabel);
    }
}
